﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Interface;
using Domain.Model;
using Domain.Model.Domain_MoBan_Model;
using Ds.DataBase;
using Ds.DataBase.Enum;
using Ds.DataBase.Model;
using Newtonsoft.Json;

namespace Domain.RealiZation
{
    public class MoBantmpl : IMoBan
    {
        public void AddGongZuoJiHua(string str)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                GongZuoJiHua data = JsonConvert.DeserializeObject<GongZuoJiHua>(str);
                db.GongZuoJiHuaContext.Add(data);
                db.SaveChanges();
            }
        }

        public void UpdateGongZuoJiHua(string str)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                GongZuoJiHua data = JsonConvert.DeserializeObject<GongZuoJiHua>(str);
                var model = db.GongZuoJiHuaContext.SingleOrDefault(s => s.mbid == data.mbid);
                model.mbmc = data.mbmc;
                foreach (var item in data.GongZuoJiHuaNeiRongs)
                {
                    var itemdata = model.GongZuoJiHuaNeiRongs.SingleOrDefault(s => s.nrid == item.nrid);
                    if (itemdata == null)
                    {
                        GongZuoJiHuaNeiRong gzjh = new GongZuoJiHuaNeiRong();
                        gzjh.GongZuoJiHua = model;
                        gzjh.gzjhfz = item.gzjhfz;
                        gzjh.gzjhhtml = item.gzjhhtml;
                        gzjh.gzjhsx = item.gzjhsx;
                        gzjh.nr = item.nr;
                        gzjh.isedit = item.isedit;
                        gzjh.GongZuoJiHuaSons = new List<Ds.DataBase.Model.GongZuoJiHuaSon>();
                        foreach (var item2 in item.GongZuoJiHuaSons)
                        {
                            Ds.DataBase.Model.GongZuoJiHuaSon son = new Ds.DataBase.Model.GongZuoJiHuaSon();
                            son.gzjhsfz = item2.gzjhsfz;
                            son.gzjhshtml = item2.gzjhshtml;
                            son.gzjhsnr = item2.gzjhsnr;
                            son.gzjhssx = item2.gzjhssx;
                            son.GongZuoJiHuaNeiRong = gzjh;
                            gzjh.GongZuoJiHuaSons.Add(son);
                        }
                        model.GongZuoJiHuaNeiRongs.Add(gzjh);
                    }
                }
                db.SaveChanges();
            }
        }

        public void DelMoBan(int mbid, int mblx)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                if (mblx == 1)
                {
                    var model = db.GongZuoJiHuaContext.FirstOrDefault(s => s.mbid == mbid && s.mbtype == (模板类型)mblx);
                    foreach (var item in model.GongZuoJiHuaNeiRongs.ToArray())
                    {
                        foreach (var item2 in item.GongZuoJiHuaSons.ToArray())
                        {
                            db.Entry(item2).State = EntityState.Deleted;
                        }

                        db.Entry(item).State = EntityState.Deleted;
                    }

                    foreach (var item in model.ZhiWeis.ToArray())
                    {
                        model.ZhiWeis.Remove(item);
                    }
                    db.Entry(model).State = EntityState.Deleted;
                    db.SaveChanges();
                }
                else
                {
                    var model = db.JiXiaoContext.FirstOrDefault(s => s.mbid == mbid && s.mbtype == (模板类型)mblx);
                    foreach (var item in model.JiXiaoNeiRongs.ToArray())
                    {
                        foreach (var item2 in item.JiXiaoSons.ToArray())
                        {
                            db.Entry(item2).State = EntityState.Deleted;
                        }

                        db.Entry(item).State = EntityState.Deleted;
                    }

                    foreach (var item in model.ZhiWeis.ToArray())
                    {
                        model.ZhiWeis.Remove(item);
                    }
                    db.Entry(model).State = EntityState.Deleted;
                    db.SaveChanges();
                }

            }
        }

        public string GetGongZuoJiHuas()
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.GongZuoJiHuaContext.Where(s => s.mbtype == 模板类型.工作计划);
                List<Domain_GongZuoJiHua> lst = new List<Domain_GongZuoJiHua>();
                foreach (var item in data.OrderByDescending(s => s.cjsj))
                {
                    Domain_GongZuoJiHua model = new Domain_GongZuoJiHua();
                    model.mbmc = item.mbmc;
                    model.mbid = item.mbid;
                    model.cjrname = item.cjrname;
                    model.cjsj = item.cjsj;
                    lst.Add(model);
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public void AddJiXiao(string str)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                JiXiao data = JsonConvert.DeserializeObject<JiXiao>(str);
                db.JiXiaoContext.Add(data);
                db.SaveChanges();
            }
        }

        public void UpdateJiXiao(string str)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                JiXiao data = JsonConvert.DeserializeObject<JiXiao>(str);
                var model = db.JiXiaoContext.SingleOrDefault(s => s.mbid == data.mbid);
                model.mbmc = data.mbmc;
                foreach (var item in data.JiXiaoNeiRongs)
                {
                    var itemdata = model.JiXiaoNeiRongs.SingleOrDefault(s => s.nrid == item.nrid);
                    if (itemdata == null)
                    {
                        JiXiaoNeiRong jxnr = new JiXiaoNeiRong();
                        jxnr.JiXiao = model;
                        jxnr.jxfz = item.jxfz;
                        jxnr.jxhtml = item.jxhtml;
                        jxnr.jxsx = item.jxsx;
                        jxnr.nr = item.nr;
                        jxnr.JiXiaoSons = new List<JiXiaoSon>();
                        foreach (var jiXiaoSon in item.JiXiaoSons)
                        {
                            jxnr.JiXiaoSons.Add(new JiXiaoSon() { JiXiaoNeiRong = jxnr, jxfz = jiXiaoSon.jxfz, jxshtml = String.Empty, jxsnr = jiXiaoSon.jxsnr, jxssx = jiXiaoSon.jxssx });
                        }
                        data.JiXiaoNeiRongs.Add(jxnr);
                    }
                    else
                    {

                        itemdata.JiXiao = model;
                        itemdata.jxfz = item.jxfz;
                        itemdata.jxhtml = item.jxhtml;
                        itemdata.jxsx = item.jxsx;
                        itemdata.nr = item.nr;
                        foreach (var jiXiaoSon in item.JiXiaoSons)
                        {
                            var sondata = itemdata.JiXiaoSons.SingleOrDefault(s => s.jxsid == jiXiaoSon.jxsid);
                            if (sondata == null)
                            {
                                sondata = (new JiXiaoSon() { JiXiaoNeiRong = itemdata, jxfz = jiXiaoSon.jxfz, jxshtml = String.Empty, jxsnr = jiXiaoSon.jxsnr, jxssx = jiXiaoSon.jxssx });
                                itemdata.JiXiaoSons.Add(sondata);
                            }
                            else
                            {
                                sondata.jxfz = jiXiaoSon.jxfz;
                                sondata.jxsnr = jiXiaoSon.jxsnr;
                                sondata.jxssx = jiXiaoSon.jxssx;
                                sondata.jxshtml = jiXiaoSon.jxshtml;
                            }
                        }
                    }
                }
                db.SaveChanges();
            }
        }

        public string GetJiXiaos()
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.JiXiaoContext;
                List<Domain_JiXiao> lst = new List<Domain_JiXiao>();
                foreach (var item in data)
                {
                    Domain_JiXiao model = new Domain_JiXiao();
                    model.mbmc = item.mbmc;
                    model.mbid = item.mbid;
                    model.cjrname = item.cjrname;
                    model.jxtype = (int)item.jxtype;
                    model.cjsj = item.cjsj;
                    lst.Add(model);
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetMoBanKaoHeFanWei(int nrid, string username)
        {
            //using (UserInfoClassContext db = new UserInfoClassContext())
            //{
            //    var data = db.KaoHeFanWeisContext.SingleOrDefault(s => s.username == username && s.NeiRong.nrid == nrid);
            //    List<Domain_KeShi> lst = new List<Domain_KeShi>();
            //    foreach (var item2 in data.ZhiWeis)
            //    {
            //        if (lst.Any(s => s.ksid == item2.keshi.ksid))
            //        {
            //            var _d = lst.SingleOrDefault(s => s.ksid == item2.keshi.ksid);
            //            _d.ZhiWus.Add(new Domain_ZhiWu()
            //            {
            //                zwmc = item2.zwmc,
            //                zwid = item2.zwid
            //            });
            //        }
            //        else
            //        {
            //            Domain_KeShi model = new Domain_KeShi();
            //            model.ZhiWus = new List<Domain_ZhiWu>();
            //            model.ksid = item2.keshi.ksid;
            //            model.ksmc = item2.keshi.ksmc;
            //            model.ZhiWus.Add(new Domain_ZhiWu()
            //            {
            //                zwmc = item2.zwmc,
            //                zwid = item2.zwid
            //            });
            //            lst.Add(model);
            //        }
            //    }
            //    return JsonConvert.SerializeObject(lst);
            //}
            return string.Empty;
        }

        public string GetGongZuoJiHua(int mbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.GongZuoJiHuaContext.SingleOrDefault(s => s.mbid == mbid);
                Domain_GongZuoJiHua model = new Domain_GongZuoJiHua();
                model.GongZuoJiHuaNeiRongs = new List<Domain_GongZuoJiHuaNeiRong>();
                model.mbmc = data.mbmc;
                model.mbid = data.mbid;
                model.cjrname = data.cjrname;
                model.mbtype = (int)data.mbtype;
                model.cjsj = data.cjsj;
                foreach (var item in data.GongZuoJiHuaNeiRongs)
                {
                    Domain_GongZuoJiHuaNeiRong _Nr = new Domain_GongZuoJiHuaNeiRong();
                    _Nr.gzjhfz = item.gzjhfz;
                    _Nr.gzjhhtml = item.gzjhhtml;
                    _Nr.gzjhsx = item.gzjhsx;
                    _Nr.nr = item.nr;
                    _Nr.nrid = item.nrid;
                    _Nr.isedit = item.isedit;
                    _Nr.GongZuoJiHuaSons = new List<Model.GongZuoJiHuaSon>();
                    foreach (var item2 in item.GongZuoJiHuaSons)
                    {
                        Model.GongZuoJiHuaSon son = new Model.GongZuoJiHuaSon();
                        son.gzbz = item2.gzbz;
                        son.gzjhsfz = item2.gzjhsfz;
                        son.gzjhshtml = item2.gzjhshtml;
                        son.gzjhsid = item2.gzjhsid;
                        son.gzjhsnr = item2.gzjhsnr;
                        son.gzjhssx = item2.gzjhssx;
                        _Nr.GongZuoJiHuaSons.Add(son);
                    }
                    model.GongZuoJiHuaNeiRongs.Add(_Nr);
                }
                return JsonConvert.SerializeObject(model);
            }
        }

        public string GetJiXiao(int mbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.JiXiaoContext.SingleOrDefault(s => s.mbid == mbid);
                Domain_JiXiao model = new Domain_JiXiao();
                model.mbmc = data.mbmc;
                model.mbid = data.mbid;
                model.cjrname = data.cjrname;
                model.jxtype = (int)data.jxtype;
                model.cjsj = data.cjsj;
                model.JiXiaoNeiRongs = new List<Domain_JiXiaoNeiRong>();
                foreach (var item in data.JiXiaoNeiRongs)
                {
                    Domain_JiXiaoNeiRong _jx = new Domain_JiXiaoNeiRong();
                    _jx.JiXiaoSons = new List<Domain_JiXiaoSon>();
                    _jx.jxfz = item.jxfz;
                    _jx.jxhtml = item.jxhtml;
                    _jx.jxsx = item.jxsx;
                    _jx.nr = item.nr;
                    _jx.nrid = item.nrid;
                    foreach (var item2 in item.JiXiaoSons)
                    {
                        Domain_JiXiaoSon _son = new Domain_JiXiaoSon();
                        _son.jxsid = item2.jxsid;
                        _son.jxfz = item2.jxfz;
                        _son.jxshtml = item2.jxshtml;
                        _son.jxsnr = item2.jxsnr;
                        _son.jxssx = item2.jxssx;
                        _jx.JiXiaoSons.Add(_son);
                    }
                    model.JiXiaoNeiRongs.Add(_jx);
                }
                return JsonConvert.SerializeObject(model);
            }
        }

        public void AddKaoHeFanWeiZhiWu(int nrid, int zwid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                if (!db.KaoHeFanWeisContext.Any(s =>
                    s.ZhiWeis.Any(p => p.zwid == zwid) && s.username == username && s.NeiRong.nrid == nrid))
                {
                    KaoHeFanWei model = new KaoHeFanWei();
                    if (db.KaoHeFanWeisContext.Any(s => s.NeiRong.nrid == nrid && s.username == username))
                    {
                        model = db.KaoHeFanWeisContext.SingleOrDefault(s =>
                            s.NeiRong.nrid == nrid && s.username == username);
                        model.ZhiWeis.Add(db.ZhiWeiContext.SingleOrDefault(s => s.zwid == zwid));
                    }
                    else
                    {
                        model.ZhiWeis = new List<ZhiWei>();
                        var data = new NeiRong();
                        var nerong = from s in db.NeiRongContext
                                     where s.nrid == nrid
                                     select s;
                        foreach (var item in nerong)
                        {
                            if (item.nrid == nrid)
                            {
                                data = item;
                            }
                        }
                        model.NeiRong = data;
                        model.username = username;
                        model.ZhiWeis.Add(db.ZhiWeiContext.SingleOrDefault(s => s.zwid == zwid));
                        db.KaoHeFanWeisContext.Add(model);
                    }

                    db.SaveChanges();
                }
            }
        }

        public void DelKaoHeFanWeiKeShi(int nrid, string zwids, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                List<int> tmp = zwids.Split('|').ToList().Select(int.Parse).ToList();
                var data = db.KaoHeFanWeisContext.SingleOrDefault(s =>
                    s.NeiRong.nrid == nrid && s.username == username);
                foreach (var item in data.ZhiWeis.ToList())
                {
                    if (tmp.Contains(item.zwid))
                    {
                        data.ZhiWeis.Remove(item);
                    }
                }
                db.SaveChanges();
            }
        }

        public string GetMoBanKaoHeFanWeiRenYuan(int nrid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                //var data = db.KaoHeFanWeiRenYuansContext.SingleOrDefault(s => s.username == username && s.NeiRong.nrid == nrid);
                List<Domain_JiaoZhiGong> lst = new List<Domain_JiaoZhiGong>();
                //foreach (var item2 in data.JiaoZhiGongInfos)
                //{
                //    Domain_JiaoZhiGong model = new Domain_JiaoZhiGong();
                //    var ry = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == item2.Users.username);
                //    if (ry != null)
                //    {
                //        model.username = ry.Users.username;
                //        model.XingMing = ry.XingMing;
                //        lst.Add(model);
                //    }
                //}
                return JsonConvert.SerializeObject(lst);
            }
        }

        public void AddKaoHeFanWeiRenYuan(int nrid, string ry, string username)
        {
            //using (UserInfoClassContext db = new UserInfoClassContext())
            //{
            //    var tmp = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == ry);
            //    KaoHeFanWei_RenYuan model = new KaoHeFanWei_RenYuan();
            //    if (db.KaoHeFanWeiRenYuansContext.Any(s => s.NeiRong.nrid == nrid && s.username == username))
            //    {
            //        model = db.KaoHeFanWeiRenYuansContext.SingleOrDefault(s =>
            //            s.NeiRong.nrid == nrid && s.username == username);

            //        model.JiaoZhiGongInfos.Add(tmp);
            //    }
            //    else
            //    {
            //        model.JiaoZhiGongInfos = new List<JiaoZhiGongInfo>();
            //        var data = new NeiRong();
            //        var nerong = from s in db.NeiRongContext
            //                     where s.nrid == nrid
            //                     select s;
            //        foreach (var item in nerong)
            //        {
            //            if (item.nrid == nrid)
            //            {
            //                data = item;
            //            }
            //        }
            //        model.NeiRong = data;
            //        model.username = username;
            //        model.JiaoZhiGongInfos.Add(tmp);
            //        db.KaoHeFanWeiRenYuansContext.Add(model);
            //    }
            //    db.SaveChanges();

            //}
        }

        public void DelKaoHeFanWeiRenYuan(int nrid, string rys, string username)
        {
            //using (UserInfoClassContext db = new UserInfoClassContext())
            //{
            //    List<string> tmp = rys.Split('|').ToList();
            //    var data = db.KaoHeFanWeiRenYuansContext.SingleOrDefault(s =>
            //        s.NeiRong.nrid == nrid && s.username == username);
            //    foreach (var item in data.JiaoZhiGongInfos.ToArray())
            //    {
            //        if (tmp.Contains(item.Users.username))
            //        {
            //            data.JiaoZhiGongInfos.Remove(item);
            //        }
            //    }
            //    db.SaveChanges();
            //}
        }

        public string GetZhuiSu(string username, int mkhbid, int oneid, int twoid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var mkhb = db.MyKaoHeBiaoContext.First(s => s.mkhbid == mkhbid);
                var onemodel = mkhb.MyKaoHeBiaoOnes.First(s => s.oneid == oneid);
                var twomodel = onemodel.MyKaoHeBiaoTwos.First(s => s.twoid == twoid);
                Domain_GongZuoZhuiSu model = new Domain_GongZuoZhuiSu();
                model.twoid = twomodel.twoid;
                model.gzcdr = twomodel.gzcdr;
                model.gznw = twomodel.mbnr;
                model.lstson = new List<Domain_GongZuoZhuiSu>();
                var user_info = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                if (twomodel.gzcdrid != user_info.Users.userinfoId)//如果工作承担人不是本人
                {
                    user_info = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.userinfoId == twomodel.gzcdrid);
                    foreach (var item in db.MyKaoHeBiaoContext.FirstOrDefault(s => s.sssj.Year == mkhb.sssj.Year && s.sssj.Month == mkhb.sssj.Month && s.username == user_info.Users.username).MyKaoHeBiaoOnes)
                    {
                        foreach (var item2 in item.MyKaoHeBiaoTwos)
                        {
                            if (item2.lyid == twomodel.twoid)
                            {

                                var model2 = new Domain_GongZuoZhuiSu();
                                model2.twoid = item2.twoid;
                                model2.gzcdr = item2.gzcdr;
                                model2.gznw = item2.mbnr;
                                model2.lstson = new List<Domain_GongZuoZhuiSu>();
                                if (item2.gzcdrid != user_info.Users.userinfoId)
                                {

                                    foreach (var item3 in db.MyKaoHeBiaoContext.FirstOrDefault(s => s.sssj.Year == mkhb.sssj.Year && s.sssj.Month == mkhb.sssj.Month && s.username == user_info.Users.username).MyKaoHeBiaoOnes)
                                    {
                                        foreach (var item4 in item3.MyKaoHeBiaoTwos)
                                        {
                                            if (item4.lyid == item2.twoid)
                                            {
                                                model2.lstson.Add(new Domain_GongZuoZhuiSu()
                                                {
                                                    twoid = item4.twoid,
                                                    gzcdr = item4.gzcdr,
                                                    gznw = item4.mbnr,
                                                    lstson = new List<Domain_GongZuoZhuiSu>()
                                                });
                                            }
                                            if (item4.xiangguangongzuoji.Count > 0)
                                            {
                                                foreach (var item5 in item4.xiangguangongzuoji)
                                                {
                                                    model2.lstson.Add(new Domain_GongZuoZhuiSu()
                                                    {
                                                        gznw = item5.xggz,
                                                        twoid = item5.xggzid
                                                    });
                                                }
                                            }
                                        }
                                    }
                                }
                                model.lstson.Add(model2);
                            }
                            if (item2.xiangguangongzuoji.Count > 0)
                            {
                                foreach (var item3 in item2.xiangguangongzuoji)
                                {
                                    model.lstson.Add(new Domain_GongZuoZhuiSu()
                                    {
                                        gznw = item3.xggz,
                                        twoid = item3.xggzid
                                    });
                                }

                            }
                        }
                    }
                }
                if (twomodel.xiangguangongzuoji.Count > 0)
                {
                    foreach (var item in twomodel.xiangguangongzuoji)
                    {
                        model.lstson.Add(new Domain_GongZuoZhuiSu()
                        {
                            gznw = item.xggz,
                            twoid = item.xggzid
                        });
                    }
                }
                return JsonConvert.SerializeObject(model);
            }
        }

        public string GetMoBanControl(int mobanpathid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var moban = db.Moban_MainContext;
                var controlid = 0;
                foreach (var s in moban)
                {
                    foreach (var s1 in s.MoBan_Paths)
                    {
                        if (s1.ljid == mobanpathid)
                        {
                            controlid = s1.ControlID;
                            break;
                        }
                    }
                    if (controlid > 0)
                    {
                        break;
                    }
                }
                var conterol = db.MoBan_ControlContext.SingleOrDefault(s => s.id == controlid);
                var html = conterol.controlhtml;
                return html;
            }
        }

        public string GetControlAll()
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var result = db.MoBan_ControlContext;
                return JsonConvert.SerializeObject(result);
            }
        }

        public int AddMoBanAnswer(int mbid, int ljid, int mobancontentid, string answer)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var result = JsonConvert.DeserializeObject<MoBan_Answer>(answer);
                var moban = db.Moban_MainContext.FirstOrDefault(s => s.mbid == mbid);
                MoBan_Content g2 = new MoBan_Content();
                var s1 = moban.MoBan_Paths.SingleOrDefault(s => s.ljid == ljid);
                g2 = s1.MoBan_Contents.SingleOrDefault(s => s.nrid == mobancontentid);
                result.html = db.MoBan_ControlContext.SingleOrDefault(s => s.id == g2.ControlID).controlhtml;
                //result.html = result.html.Replace("valueattr", "value=" + result.fenzhi);
                //result.html = result.html.Replace("nameattr", "name=" + g2.nrid);
                var t1 = new MoBan_Answer()
                {
                    html = result.html,
                    MoBanContent = g2,
                    fenzhi = result.fenzhi,
                    tips = result.tips,
                    shunxu = result.shunxu
                };
                g2.MoBan_Answers.Add(t1);
                db.SaveChanges();
                return t1.aid;
            }
        }

        public void UpdateMoBanAnswer(int mbid, int ljid, int mobancontentid, string answer)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var result = JsonConvert.DeserializeObject<MoBan_Answer>(answer);
                var moban = db.Moban_MainContext.FirstOrDefault(s => s.mbid == mbid);
                MoBan_Content g2 = new MoBan_Content();
                var s1 = moban.MoBan_Paths.SingleOrDefault(s => s.ljid == ljid);
                g2 = s1.MoBan_Contents.SingleOrDefault(s => s.nrid == mobancontentid);
                result.html = db.MoBan_ControlContext.SingleOrDefault(s => s.id == g2.ControlID).controlhtml;
                //result.html = result.html.Replace("valueattr", "value=" + result.fenzhi);
                //result.html = result.html.Replace("nameattr", "name=" + g2.nrid);
                var m1 = g2.MoBan_Answers.SingleOrDefault(m => m.aid == result.aid);
                m1.html = result.html;
                m1.fenzhi = result.fenzhi;
                m1.tips = result.tips;
                m1.shunxu = result.shunxu;
                db.SaveChanges();
            }
        }

        public void DeleteMoBanAnswer(int mbid, int ljid, int nrid, int answerid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var moban = db.Moban_MainContext.FirstOrDefault(s => s.mbid == mbid);
                MoBan_Content g2 = new MoBan_Content();
                var s1 = moban.MoBan_Paths.SingleOrDefault(s => s.ljid == ljid);
                var t1 = s1.MoBan_Contents.SingleOrDefault(s => s.nrid == nrid);
                var m1 = t1.MoBan_Answers.SingleOrDefault(m => m.aid == answerid);
                t1.MoBan_Answers.Remove(m1);
                db.Entry(m1).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public void AddMoBan(string name, string cjrid, int mbtype)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                MoBan_Main moBan = new MoBan_Main();
                moBan.mbmc = name;
                moBan.cjr = Guid.Parse(cjrid);
                moBan.isuse = true;
                moBan.mbtype = (模板类型)mbtype;
                db.Moban_MainContext.Add(moBan);
                db.SaveChanges();
            }
        }

        public void UpdateMoBan(int mbid, string name)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                MoBan_Main moban = db.Moban_MainContext.FirstOrDefault(s => s.mbid == mbid);
                moban.mbmc = name;
                db.SaveChanges();
            }
        }

        public void DelMoBan(int mbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                MoBan_Main moban = db.Moban_MainContext.Include("MoBan_Paths").Select(s => s).SingleOrDefault(s => s.mbid == mbid);
                foreach (var item in moban.MoBan_Paths.ToList())
                {
                    foreach (var item2 in item.MoBan_Contents.ToList())
                    {
                        foreach (var item3 in item2.MoBan_Answers.ToList())
                        {
                            db.Entry(item3).State = EntityState.Deleted;
                        }
                        db.Entry(item2).State = EntityState.Deleted;
                    }
                    db.Entry(item).State = EntityState.Deleted;
                }
                db.Moban_MainContext.Remove(moban);
                db.SaveChanges();
            }
        }

        public List<Domain_MoBan_Main> GetAllMoBan()
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {

                var data = db.Moban_MainContext.ToList();
                var result = AutoMapper.Mapper.Map<List<Domain_MoBan_Main>>(data);
                //foreach (var item in data)
                //{
                //    var model = item.MapTo<Domain_MoBan_Main>();
                //}

                //foreach (var item in result)
                //{
                //    var a = AutoMapper.Mapper.Map<Domain_MoBan_Main>(item);
                //}
                return result;
            }

        }

        public string GetExceptMySelfMoBan(int mbid, string keyword)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var result = db.Moban_MainContext.Where(s => s.mbid != mbid && (string.IsNullOrEmpty(keyword) || s.mbmc.Contains(keyword)));
                return JsonConvert.SerializeObject(result);
            }
        }

        public string GetMoBanInfo(int mbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var result = db.Moban_MainContext.SingleOrDefault(s => s.mbid == mbid);
                return JsonConvert.SerializeObject(result);
            }
        }

        public int AddMoBanPath(int mbid, int sx, string name, int pid, decimal fenzhi, bool isedit, int controlid, bool isinclude,
            int sonmobanid = 0)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                MoBan_Main moban = db.Moban_MainContext.SingleOrDefault(s => s.mbid == mbid);
                MoBan_Path mobanpath = new MoBan_Path();
                mobanpath.shunxu = sx;
                mobanpath.fenzhi = fenzhi;
                mobanpath.ControlID = controlid;
                mobanpath.isedit = isedit;
                mobanpath.isinclude = isinclude;
                mobanpath.ljmc = name;
                //mobanpath.ParentMoBanMain = moban;
                mobanpath.pid = pid;
                if (isinclude)
                {
                    mobanpath.SonMoBanMain = db.Moban_MainContext.SingleOrDefault(s => s.mbid == sonmobanid);
                }
                else
                {
                    mobanpath.SonMoBanMain = null;
                }
                moban.MoBan_Paths.Add(mobanpath);
                db.SaveChanges();
                mobanpath.paths = mobanpath.ljid + "/";
                if (mobanpath.pid > 0)
                {
                    mobanpath.paths = mobanpath.paths + getpaths(moban.MoBan_Paths.ToList(), mobanpath.pid);
                }
                db.SaveChanges();
                return mobanpath.ljid;
            }
        }
        /// <summary>
        /// 获取路径的递归
        /// </summary>
        /// <param name="MoBan_Paths"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        string getpaths(List<MoBan_Path> MoBan_Paths, int pid)
        {
            var mbpath = MoBan_Paths.SingleOrDefault(s => s.ljid == pid);
            if (mbpath.pid == 0)
            {
                return mbpath.ljid + "/";
            }
            else
            {
                return mbpath.ljid + "/" + getpaths(MoBan_Paths, mbpath.pid);
            }
        }

        public void UpdateMoBanPath(int mbid, int pathid, int sx, string name, decimal fenzhi, bool isedit, int controlid,
            bool isinclude, int sonmobanid = 0)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                MoBan_Main moban = db.Moban_MainContext.SingleOrDefault(s => s.mbid == mbid);
                MoBan_Path mobanpath = moban.MoBan_Paths.SingleOrDefault(s => s.ljid == pathid);
                mobanpath.shunxu = sx;
                mobanpath.fenzhi = fenzhi;
                mobanpath.ControlID = controlid;
                mobanpath.isedit = isedit;
                mobanpath.ljmc = name;
                mobanpath.isinclude = isinclude;
                if (isinclude)
                {
                    mobanpath.SonMoBanMain = db.Moban_MainContext.SingleOrDefault(s => s.mbid == sonmobanid);
                }
                else
                {
                    mobanpath.SonMoBanMain = null;
                }
                mobanpath.paths = mobanpath.ljid + "/";
                if (mobanpath.pid > 0)
                {
                    mobanpath.paths = mobanpath.paths + getpaths(moban.MoBan_Paths.ToList(), mobanpath.pid);
                }
                db.SaveChanges();
            }
        }

        public void DelMoBanPath(int mbid, int ljid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                //db.Moban_MainContext.In("", "", "");
                MoBan_Main moban = db.Moban_MainContext.Select(s => s).SingleOrDefault(s => s.mbid == mbid);
                MoBan_Path mobanpath = moban.MoBan_Paths.Select(s => s).SingleOrDefault(s => s.ljid == ljid);
                //moban.MoBan_Paths.Remove(mobanpath);
                foreach (var item in moban.MoBan_Paths.ToList())
                {
                    var arr = item.paths.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList().Select(int.Parse);
                    if (arr.Any(s => s == ljid))
                    {
                        foreach (var item2 in item.MoBan_Contents.ToList())
                        {
                            foreach (var item3 in item2.MoBan_Answers.ToList())
                            {
                                db.Entry(item3).State = EntityState.Deleted;
                            }

                            db.Entry(item2).State = EntityState.Deleted;
                        }
                        db.Entry(item).State = EntityState.Deleted;
                    }
                }
                foreach (var item2 in mobanpath.MoBan_Contents.ToList())
                {
                    foreach (var item3 in item2.MoBan_Answers.ToList())
                    {
                        db.Entry(item3).State = EntityState.Deleted;
                    }

                    db.Entry(item2).State = EntityState.Deleted;
                }

                db.Entry(mobanpath).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public int AddMoBanContent(int mbid, int pathid, int sx, string contents, decimal fenzhi, int controlid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                MoBan_Content moBanContent = new MoBan_Content();
                MoBan_Main moban = db.Moban_MainContext.SingleOrDefault(s => s.mbid == mbid);
                MoBan_Path mobanpath = moban.MoBan_Paths.SingleOrDefault(s => s.ljid == pathid);
                moBanContent.ControlID = controlid;
                moBanContent.fenzhi = fenzhi;
                moBanContent.nr = contents;
                moBanContent.shunxu = sx;
                mobanpath.MoBan_Contents.Add(moBanContent);
                db.SaveChanges();
                return moBanContent.nrid;
            }
        }

        public void UpdateMoBanContent(int mbid, int ljid, int contentid, int sx, string contents, decimal fenzhi, int controlid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                MoBan_Content moBanContent = new MoBan_Content();
                MoBan_Main moban = db.Moban_MainContext.SingleOrDefault(s => s.mbid == mbid);

                var item = moban.MoBan_Paths.SingleOrDefault(s => s.ljid == ljid);
                moBanContent = item.MoBan_Contents.SingleOrDefault(s => s.nrid == contentid);
                moBanContent.ControlID = controlid;
                moBanContent.fenzhi = fenzhi;
                moBanContent.nr = contents;
                moBanContent.shunxu = sx;
                db.SaveChanges();
            }
        }

        public void DelMoBanContent(int mbid, int ljid, int nrid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                MoBan_Content moBanContent = new MoBan_Content();
                MoBan_Main moban = db.Moban_MainContext.SingleOrDefault(s => s.mbid == mbid);
                var item = moban.MoBan_Paths.SingleOrDefault(s => s.ljid == ljid);
                moBanContent = item.MoBan_Contents.Select(s => s).SingleOrDefault(s => s.nrid == nrid);
                foreach (var item2 in moBanContent.MoBan_Answers.ToList())
                {
                    db.Entry(item2).State = EntityState.Deleted;
                }
                item.MoBan_Contents.Remove(moBanContent);
                db.Entry(moBanContent).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public string GetSearchMoBanList(string keyword)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var result = db.Moban_MainContext.Where(s => s.mbmc.Contains(keyword));
                return JsonConvert.SerializeObject(result);
            }
        }

        public string GetMoBanType()
        {
            Dictionary<string, int> mydic = new Dictionary<string, int>();

            foreach (模板类型 foo in Enum.GetValues(typeof(模板类型)))
            {
                mydic.Add(foo.ToString(), (int)foo);
            }
            return JsonConvert.SerializeObject(mydic);
        }

        public string GetKaoHeFanWeis(string name)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = from khfw in db.KaoHeFanWeiRenYuansContext
                           join jzg in db.JiaoZhiGongInfoContext on khfw.username equals jzg.Users.username into temp
                           from tt in temp.DefaultIfEmpty()
                           where tt.XingMing.Equals(name) || string.IsNullOrEmpty(name)
                           select new { username = khfw.username, xingming = tt.XingMing, userinfoId = tt.Users.userinfoId, NeiRongs = khfw.NeiRongs, khfwryid = khfw.khfwryid, BeiKaoHeRenYuans = khfw.JiaoZhiGongInfos };
                return JsonConvert.SerializeObject(data.Where(s=>s.userinfoId!=null).ToList());
            }
        }

        public string GetKaoHeFanWei(int khfwryid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                //var data = from khfw in db.KaoHeFanWeiRenYuansContext where khfw.khfwryid == khfwryid select khfw;
                var data = from khfw in db.KaoHeFanWeiRenYuansContext
                           join jzg in db.JiaoZhiGongInfoContext on khfw.username equals jzg.Users.username into temp
                           from tt in temp.DefaultIfEmpty()
                           where khfw.khfwryid == khfwryid
                           select new { username = khfw.username, xingming = tt.XingMing, userinfoId = tt.Users.userinfoId, NeiRongs = khfw.NeiRongs, khfwryid = khfw.khfwryid, BeiKaoHeRenYuans = khfw.JiaoZhiGongInfos };
                if (data != null)
                {
                    return JsonConvert.SerializeObject(data.First());
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public int AddKaoHeFanWei(string userinfoid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                KaoHeFanWei_RenYuan khfw = new KaoHeFanWei_RenYuan();
                Guid guid = Guid.Parse(userinfoid);
                var jzg = db.JiaoZhiGongInfoContext.Single(s => s.Users.userinfoId == guid);
                khfw.username = jzg.Users.username;
                db.KaoHeFanWeiRenYuansContext.Add(khfw);
                db.SaveChanges();
                return khfw.khfwryid;
            }
        }

        public void AddKaoHePath(int khfwryid, int ljid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.KaoHeFanWeiRenYuansContext.Single(s => s.khfwryid == khfwryid);
                var t1 = db.Moban_MainContext.ToList();
                var moban = t1.SingleOrDefault(s => s.MoBan_Paths.Any(y => y.ljid == ljid));
                var path = moban.MoBan_Paths.SingleOrDefault(s => s.ljid == ljid);
                if (data.NeiRongs == null)
                {
                    data.NeiRongs = new List<NeiRong>();
                }
                data.NeiRongs.Add(new NeiRong() { mbid = moban.mbid, mbmc = moban.mbmc, nr = path.ljmc, ljid = path.ljid });

                db.SaveChanges();
            }
        }

        public void DelKaoHePath(int khfwryid, int nrid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.KaoHeFanWeiRenYuansContext.Single(s => s.khfwryid == khfwryid);
                foreach (var item in data.NeiRongs.ToList())
                {
                    if (item.nrid == nrid)
                    {
                        db.Entry(item).State = EntityState.Deleted;
                    }
                }

                db.SaveChanges();
            }
        }

        public void AddBeiKaoHeJiaoZhiGong(int khfwryid, string userid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.KaoHeFanWeiRenYuansContext.Single(s => s.khfwryid == khfwryid);
                Guid guid = Guid.Parse(userid);
                var ry = db.JiaoZhiGongInfoContext.Single(s => s.Users.userinfoId == guid);
                db.KeShiZhiWuContext.ToList();
                db.ZhiWeiContext.ToList();
                var bry = new BeiKaoHeJiaoZhiGong();
                bry.username = ry.Users.username;
                bry.userinfoId = ry.Users.userinfoId.ToString();
                bry.XingMing = ry.XingMing;
                bry.XingBie = ry.XingBie;
                bry.ksmc = ry.keshi.ksmc;
                if (ry.kszw == null)
                {
                    bry.zwmc = string.Empty;
                }
                else
                {
                    bry.zwmc = ry.kszw.zhiwu.zwmc;
                }

                if (data.JiaoZhiGongInfos == null)
                {
                    data.JiaoZhiGongInfos = new List<BeiKaoHeJiaoZhiGong>();
                }
                data.JiaoZhiGongInfos.Add(bry);
                db.SaveChanges();
            }
        }

        public void AddBeiKaoHeJiaoZhiGong(int khfwryid, int ksid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.KaoHeFanWeiRenYuansContext.Single(s => s.khfwryid == khfwryid);
                var ry = db.JiaoZhiGongInfoContext.Where(s => s.keshi.ksid == ksid);
                if (data.JiaoZhiGongInfos == null)
                {
                    data.JiaoZhiGongInfos = new List<BeiKaoHeJiaoZhiGong>();
                }
                foreach (var item in ry)
                {
                    var bry = new BeiKaoHeJiaoZhiGong();
                    bry.username = item.Users.username;
                    bry.userinfoId = item.Users.userinfoId.ToString();
                    bry.XingMing = item.XingMing;
                    bry.XingBie = item.XingBie;
                    bry.ksmc = item.keshi.ksmc;
                    bry.zwmc = item.kszw.zhiwu.zwmc;
                    data.JiaoZhiGongInfos.Add(bry);
                }
                db.SaveChanges();
            }
        }

        public void DelBeiKaoHeJiaoZhiGong(int khfwryid, string userid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.KaoHeFanWeiRenYuansContext.Single(s => s.khfwryid == khfwryid);
                var model = data.JiaoZhiGongInfos.Single(s => s.userinfoId == userid);
                data.JiaoZhiGongInfos.Remove(model);
                db.SaveChanges();
            }
        }

        public void DelKaoHeFanWei(int khfwryid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.KaoHeFanWeiRenYuansContext.Single(s => s.khfwryid == khfwryid);
                foreach (var item in data.NeiRongs.ToList())
                {
                    db.Entry(item).State = EntityState.Deleted;
                }

                foreach (var item in data.JiaoZhiGongInfos.ToList())
                {
                    data.JiaoZhiGongInfos.Remove(item);
                }

                db.Entry(data).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }
    }
}
